package com.imooc.controller;

import com.imooc.pojo.bo.ShopcartBO;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车
 */
@Slf4j
@Api(value = "购物车", tags = {"购物车接口相关的api"})
@RestController
@RequestMapping("shopcart")
public class ShopCartController extends BaseController {

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("add")
    public IMOOCJSONResult carousel(
            @RequestParam String userId,
            @RequestBody ShopcartBO shopcartBO,
            HttpServletRequest request,
            HttpServletResponse response) {
        log.info("{}", shopcartBO);
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("");
        }

        String shopcartListCache = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        List<ShopcartBO> shopcartBOList = null;
        if (StringUtils.isNoneBlank(shopcartListCache)) {
            shopcartBOList = JsonUtils.jsonToList(shopcartListCache, ShopcartBO.class);
            //判断新加的商品是否在购物车中。默认false
            boolean isInShopcartBOList = false;
            for (ShopcartBO sc : shopcartBOList) {
                if (sc.getSpecId().equals(shopcartBO.getSpecId())) {
                    sc.setBuyCounts(sc.getBuyCounts() + shopcartBO.getBuyCounts());
                    isInShopcartBOList = true;
                    break;
                }
            }
            if (!isInShopcartBOList) {
                shopcartBOList.add(shopcartBO);
            }
        } else {
            shopcartBOList = new ArrayList<>();
            shopcartBOList.add(shopcartBO);
        }
        redisOperator.set(FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartBOList));
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "删除购物车商品", httpMethod = "POST")
    @PostMapping("del")
    public IMOOCJSONResult del(
            @RequestParam String userId,
            @RequestParam String itemSpecId,
            HttpServletRequest request,
            HttpServletResponse response) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return IMOOCJSONResult.errorMsg("");
        }

        String shopcartListCache = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        List<ShopcartBO> shopcartBOList = null;
        if (StringUtils.isNoneBlank(shopcartListCache)) {
            shopcartBOList = JsonUtils.jsonToList(shopcartListCache, ShopcartBO.class);
            shopcartBOList = shopcartBOList.stream()
                    .filter(e -> !e.getSpecId().equals(itemSpecId))
                    .collect(Collectors.toList());
            redisOperator.set(FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartBOList));
        }

        return IMOOCJSONResult.ok();
    }


}
